#!/bin/bash
# USAGE:
#	s_crop2resample
#
# DESCRIPTION:
#  S_CROP2RESAMPLE is an internal ADORE command. 
#  It is used in creating interferograms coregistered to a different scene, 
#  as needed in Small Baselines (SBAS) interferometry. 
#
#  s_crop2resample copies the result of m_ovs or the m_crop as s_resample
#  and changes the data_output_file to the s_crop(or s_ovs) output file. 
#
#  Selection between ovs and crop is done automatically. If m_ovs is in the
#  process control flag, ovs is used. Otherwise m_crop is used. 
#
#  Master and Slave should be processed in the same way. They both have to 
#  be oversampled or not.  
#
# INPUT:
#  There are no required input parameters for this command.
#  
# OUTPUT:
#   Modifies the result file by copying the m_crop(m_ovs) to s_resample 
#

function fun_crop2resample(){
copyRes.sh ${m_resfile} crop ${s_resfile} resample
local scropOut=`readRes.sh ${s_resfile} crop Data_output_file`
local mcropOut=`readRes.sh ${m_resfile} crop Data_output_file`
local scropOut_format=`readRes.sh ${s_resfile} crop Data_output_format`
local mcropOut_format=`readRes.sh ${m_resfile} crop Data_output_format`
#modifyRes.sh ${s_resfile} resample "Data_output_file" ${scropOut}
sed -i "s<${mcropOut}<${scropOut}<g" ${s_resfile}
[ "${scropOut_format}" != "${mcropOut_format}"  ] && modifyRes.sh ${s_resfile} resample "Data_output_format" ${scropOut_format} 
}
function fun_ovs2resample(){
copyRes.sh ${m_resfile} oversample ${s_resfile} resample
local scropOut=`readRes.sh ${s_resfile} oversample Data_output_file`
local mcropOut=`readRes.sh ${m_resfile} oversample Data_output_file`
local scropOut_format=`readRes.sh ${s_resfile} oversample Data_output_format`
local mcropOut_format=`readRes.sh ${m_resfile} oversample Data_output_format`
#modifyRes.sh ${s_resfile} resample "Data_output_file" ${scropOut}
sed -i "s<${mcropOut}<${scropOut}<g" ${s_resfile}
[ "${scropOut_format}" != "${mcropOut_format}"  ] && modifyRes.sh ${s_resfile} resample "Data_output_format" ${scropOut_format} 
}
###MAIN
if [ `readRes.sh ${m_resfile} process_control oversample` == "1" ]; then
  fun_ovs2resample ${@}
elif [ `readRes.sh ${m_resfile} process_control crop` == "1" ]; then
  fun_crop2resample ${@}
else
  error "s_crop2resample: m_ovs or m_crop steps are not in m_resfile."
fi
